package com.redskt.classroom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.redskt.classroom.entity.RedClassUser;
import com.redskt.classroom.entity.RedUserFocus;
import com.redskt.classroom.service.RedUserFocusService;
import com.redskt.classroom.service.RedUserService;
import com.redskt.commonutils.MD5;
import com.redskt.commonutils.RequestParmUtil;
import com.redskt.security.TokenManager;
import com.redskt.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author tanyuehong
 * @since 2020-05-16
 */
@RestController
@RequestMapping("/ucenter")
@CrossOrigin(allowCredentials="true",maxAge = 3600)
public class RedUserController {

    private static final List<String> CONTENT_TYPES = Arrays.asList("image/jpeg", "image/png");

    @Autowired
    private RedUserService userService;

    @Autowired
    private RedUserFocusService focusService;


    //根据token获取用户信息
    @GetMapping("getUserInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String uid = TokenManager.getMemberIdByJwtToken(request);

        //1 根据订单号查询订单信息
        QueryWrapper<RedClassUser> wrapper = new QueryWrapper<>();
        wrapper.select("id","username", "nickname","sex","age","sign","avatar","position","perpage","company","perintroduction","authority");
        wrapper.eq("id",uid);

        RedClassUser eduUser = userService.getOne(wrapper);
        if (eduUser != null && eduUser.getSign()==null) {
            eduUser.setSign("这位同学很懒，木有签名的说～");
        }
        return R.ok().data("userInfo",eduUser);
    }

    @PostMapping("getUserAdminState")
    public R getUserAdminState(HttpServletRequest request) {
        String uId = TokenManager.getMemberIdByJwtToken(request);
        if (uId.length()>0) {
            return R.ok().data("isAdmin",this.userService.checkIsAdmin(uId));
        } else {
            return R.error();
        }
    }

    @PostMapping("uploadUserImage")
    public  R uploadUerImage(@RequestParam(value = "file") MultipartFile file,HttpServletRequest request) {
        if (file.isEmpty()) {
            return R.error("上传失败，请选择文件");
        }
        String contentType = file.getContentType();
        if (!CONTENT_TYPES.contains(contentType)){
            // 文件类型不合法，直接返回null
            return R.error("图片格式不正确!");
        }
        String fileName = file.getOriginalFilename();
        String userName = TokenManager.getMemberIdByJwtToken(request);
        if (userName == null||userName.length()==0) {
            return R.error("用户验证失败，请登录后重试");
        }
        String userPath = MD5.getMD5(userName);
        File dest = new File("/home/redsktsource/user/"+ userPath +"/"+fileName);
        try {
            if (!dest.exists()) dest.mkdirs(); // 要是目录不存在,创建一个
            file.transferTo(dest);
            return R.ok().data("imageUrl","https://static.redskt.com/user/"+userPath+"/"+fileName);
        } catch (IOException e) {
            return R.error(e.getLocalizedMessage());
        }
    }

    @PostMapping("uploadImage")
    public  R uploadImage(@RequestParam(value = "file") MultipartFile file,HttpServletRequest request) {
        if (file.isEmpty()) {
            return R.error("上传失败，请选择文件");
        }
        String contentType = file.getContentType();
        if (!CONTENT_TYPES.contains(contentType)){
            // 文件类型不合法，直接返回null
            return R.error("图片格式不正确!");
        }
        String fileName = file.getOriginalFilename();
        String userName = TokenManager.getMemberIdByJwtToken(request);
        if (userName == null||userName.length()==0) {
            return R.error("用户验证失败，请登录后重试");
        }
        String userPath = MD5.getMD5(userName);
        File dest = new File("/home/redsktsource/user/"+ userPath +"/"+fileName);
        try {
            if (!dest.exists()) dest.mkdirs(); // 要是目录不存在,创建一个
            file.transferTo(dest);
            return R.ok().data("imageUrl","https://static.redskt.com/user/"+userPath+"/"+fileName);
        } catch (IOException e) {
            return R.error(e.getLocalizedMessage());
        }
    }

    @PostMapping("updateUserInfo")
    public R updateUserInfo(@RequestBody Map parameterMap) {
        parameterMap = RequestParmUtil.transToMAP(parameterMap);
        String userId =(String)parameterMap.get("id");
        String touxiang = (String)parameterMap.get("avatar");
        String sex = (String) parameterMap.get("sex");
        if(sex.equals("1")  && touxiang.equals("https://static.redskt.com/assets/img/yonghutouxiangnv.png")) {
            parameterMap.put("avatar","https://static.redskt.com/assets/img/yonghutouxiangnan.png");
        }
        if(sex.equals("2") && touxiang.equals("https://static.redskt.com/assets/img/yonghutouxiangnan.png")) {
            parameterMap.put("avatar","https://static.redskt.com/assets/img/yonghutouxiangnv.png");
        }
        parameterMap.remove("id");
        int count = userService.updateUserInfo(parameterMap,userId);
        Map returnMap = new HashMap();
        if (count>0) {
            returnMap.put("resultType",1);
            returnMap.put("tips","更新用户信息成功");
            return R.ok().data(returnMap);
        } else {
            returnMap.put("resultType",2);
            returnMap.put("tips","更新用户信息失败，请重试");
            return R.ok().data(returnMap);
        }
    }

    @PostMapping("changeUserPwd")
    public R changeUserPwd(@RequestBody Map parameterMap) {
        parameterMap = RequestParmUtil.transToMAP(parameterMap);
        String value =(String)parameterMap.get("newPwd");
        parameterMap.remove("newPwd");
        parameterMap.put("password",MD5.encrypt(value));

        // e10adc3949ba59abbe56e057f20f883e

        String oldPwd =(String)parameterMap.get("oldPwd");
        oldPwd = MD5.encrypt(oldPwd);
        parameterMap.remove("oldPwd");

        String userId =(String)parameterMap.get("id");
        parameterMap.remove("id");
        int count = userService.changeUserPwd(parameterMap,oldPwd,userId);
        Map returnMap = new HashMap();
        if (count>0) {
            returnMap.put("resultType",1);
            returnMap.put("tips","密码修改成功");
            return R.ok().data(returnMap);
        } else  {
            returnMap.put("resultType",2);
            returnMap.put("tips","密码验证失败");
            return R.ok().data(returnMap);
        }
    }

    @GetMapping("addUserFocus/{fId}")
    public R addUserFocus(@PathVariable String fId, HttpServletRequest request) {
        if (fId.length() > 0) {
            String uId = TokenManager.getMemberIdByJwtToken(request);
            if (uId.length() > 0) {
                int count = focusService.updateUserFocus(uId, fId);
                if (count <= 0) {
                    RedUserFocus focus = new RedUserFocus();
                    focus.setFid(fId);
                    focus.setUid(uId);
                    if (focusService.save(focus)) {
                        return R.ok().data("focus", true);
                    }
                } else {
                    return R.ok().data("focus", true);
                }
            } else {
                return R.error("登录信息异常，请重新登录后尝试！");
            }
        }
        return R.error("关注失败，请稍后重试哈！");
    }

    @GetMapping("cancleUserFocus/{fId}")
    public R cancleUserFocus(@PathVariable String fId, HttpServletRequest request) {
        if (fId.length() > 0) {
            String uId = TokenManager.getMemberIdByJwtToken(request);
            if (uId.length() > 0) {
                QueryWrapper<RedUserFocus> focusWrapper = new QueryWrapper<>();
                focusWrapper.eq("uid", uId);
                focusWrapper.eq("fid", fId);
                if (focusService.remove(focusWrapper)) {
                    return R.ok().data("focus", false);
                }
            } else {
                return R.error("登录信息异常，请重新登录后尝试！");
            }
        }
        return R.error("取消关注失败，请稍后重试哈！");
    }
}
